package cn.kgc.controller;

import cn.kgc.base.dto.LockStockDTO;
import cn.kgc.service.WmsWareInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.kgc.entity.WmsWareSku;
import cn.kgc.service.WmsWareSkuService;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 商品库存(WmsWareSku)表控制层
 *
 * @author 石浩杰
 * @since 2022-12-07 10:41:17
 */
@RestController
@RequestMapping("/wms/WareSku")
@Api
@Validated
public class WmsWareSkuController {
    /**
     * 服务对象
     */
    @Resource
    private WmsWareSkuService wmsWareSkuService;

    @Resource
    private WmsWareInfoService wmsWareInfoService;


    @PutMapping("/lockStock")
    @ApiOperation(value = "查询所有商品库存", httpMethod = "PUT", produces = "application/json;charset=utf-8", response = Result.class)
    @CrossOrigin
    public boolean lockStock(@RequestBody List<LockStockDTO> lockStockDTOList){

        return wmsWareSkuService.lockStock(lockStockDTOList);
    }

    @GetMapping("/hasStock")
    @CrossOrigin
    @ApiOperation(value = "判断是否有库存",httpMethod = "GET",produces = "application/json;charset=UTF-8",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId",value = "商品id",required = true,dataType = "Long",paramType = "query")
    })
    public boolean hasStock(@RequestParam(name = "skuId") Long skuId){
        LambdaQueryWrapper<WmsWareSku> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmsWareSku::getSkuId,skuId);
        List<WmsWareSku> list = wmsWareSkuService.list(lambdaQueryWrapper);
        long stock=0;
        long lockStock=0;
        //计算总库存和总锁定库存
        for (WmsWareSku wmsWareSku : list) {
            stock+=wmsWareSku.getStock();
            lockStock+=wmsWareSku.getStockLocked();
        }

        return stock>lockStock;
    }

    /**
     * 分页查询商品库存信息数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询商品库存信息", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "skuName", value = "skuName", paramType = "query", dataType = "string"),
    })
    @CrossOrigin
    public IPage<WmsWareSku> selectAll(@RequestParam(defaultValue = "1") int current,
                                       @RequestParam(defaultValue = "5") int pageSize,
                                       @RequestParam(defaultValue = "") String skuName) {
        LambdaQueryWrapper<WmsWareSku> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(skuName),WmsWareSku::getSkuName,skuName);
        IPage<WmsWareSku> page = new Page(current, pageSize);
        wmsWareSkuService.page(page,lambdaQueryWrapper);
        List<WmsWareSku> wmsWareSkuList=page.getRecords();
        for (WmsWareSku wmsWareSku : wmsWareSkuList) {
            wmsWareSku.setWareName(wmsWareInfoService.getById(wmsWareSku.getWareId()).getName());
        }
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public WmsWareSku selectOne(@PathVariable Serializable id) {
        return this.wmsWareSkuService.getById(id);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.wmsWareSkuService.removeById(id);
    }
}
